import { api_getStatusUserToken } from '@/api/api'
import { initOssUserClient,checkOssUserClient } from '@/utils/frontupload'

const uploadFileAliOSS = function (opt,path,bucket = 'newxuejinguser',type = 1) {
	return new Promise((resolve,reject) => {
		switch (type) {
			case 1: 
				if (opt.size >2* 1024 * 1024) {
					uni.showToast({title: `请上传小于2MB的图片`,icon: 'error'});
					return;
				}
				const tmp = opt.name.split('.');
				const extname = tmp.pop();
				const isValid = ['jpg', 'jpeg', 'png', 'gif'].includes(extname);
				if (!isValid) {
					uni.showToast({title:`只支持上传 jpg、jpeg、png、gif 格式的图片`,icon: 'error'});
					return;
				}
				break;
			case 2: 
				if (opt.size > 5 * 1024 * 1024) {
					uni.showToast({title: `请上传小于5MB的音频`,icon: 'error'});
					return;
				}
				const tmpaudio = opt.name.split('.');
				const extaudioname = tmpaudio.pop();
				const isaudioValid = ['mp3'].includes(extaudioname);
				if (!isaudioValid) {
					uni.showToast({title: `只支持上传mp3格式的音频`,icon: 'error'});
					return;
				}
				break;
			case 3: 
				if (opt.size > 5 * 1024 * 1024) {
					uni.showToast({title: `请上传小于5MB的视频`,icon: 'error'});
					return;
				}
				const tmpvideo = opt.name.split('.');
				const extvideoname = tmpvideo.pop();
				const isvideoValid = ['mp4'].includes(extvideoname);
				if (!isvideoValid) {
					uni.showToast({title: `只支持上传mp4格式的视频`,icon: 'error'});
					return;
				}
				break;
			case 4:
				if (opt.size > 5 * 1024 * 1024) {
					uni.showToast({title: `请上传小于5MB的文件`,icon: 'error'});
					return;
				}				
				const tmpfiles = opt.name.split('.');
				const extfilesname = tmpfiles.pop();
				const isfilesValid = ['txt','jpg','png','pdf','doc','xls','ppt','docx','xlsx','pptx','rar','zip'].includes(extfilesname);
				if (!isfilesValid) {
					uni.showToast({title: `仅支持txt、jpg、png、pdf、doc、xls、ppt、docx、xlsx、pptx、rar、zip格式的文件`,icon: 'error'});
					return;
				}
				break;			
		}
		if (bucket == "newxuejinguser") {
			var client = checkOssUserClient();		
			if (client === null) {
				api_getStatusUserToken({shop_name: uni.getStorageSync('shopname')}).then(res => {
					if (res.code == 1) {
						let oss = res.data;
						client = initOssUserClient(
							oss['accessKeyId'],
							oss['accessKeySecret'],
							oss['securityToken'],
							oss['expiration'],
							bucket
						);	
						uploadOSSClient(client,path,opt,resolve,reject);
					} else {
						uni.showToast({title: `请重新登录`,icon: 'error'});
						uni.redirectTo({ url: '/pages/my/index'	})
					}
				}).catch(err => {
					console.log(err)
				})
			} 
			try {			
				uploadOSSClient(client,path,opt,resolve,reject);
			} catch(error){
				uni.showToast({title: `${error}`,icon: 'error'});
			}
		}	
	})	
}

function uploadOSSClient(client,path,opt,resolve,reject){
	client.multipartUpload(path, opt, {}).then(rst => {
		if (rst.res.statusCode == 200) {
			const ossPath = rst.res.requestUrls[0].split('?')[0];
			let url = ossPath.replace('http://','https://');
			resolve(url)
		} else {
			reject(rst)
		}
	})
}

export default uploadFileAliOSS;

